/* -----------------------------------------------------------------------------
 * PROJECT    : lwf_jaxb
 * FILE       : XMLDataService.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Feb 07, 2011  mycodetalks			       Created
 */
package net.lwf.jaxb.service;

import java.util.ArrayList;
import java.util.List;

import net.lwf.jaxb.entity.BaseData;
import net.lwf.jaxb.stubs.maths.arithmatics.Arithmatics;
import net.lwf.jaxb.stubs.maths.arithmatics.Datarows;
import net.lwf.jaxb.stubs.maths.arithmatics.Decimals;
import net.lwf.jaxb.stubs.maths.arithmatics.Digits;
import net.lwf.jaxb.stubs.maths.arithmatics.Grade;
import net.lwf.jaxb.stubs.maths.arithmatics.Info;
import net.lwf.number.util.RandomNumberGenerator;

public class XMLDataService extends JaxbService {
	
	private static final String CONTEXT_PATH = "net.lwf.jaxb.stubs.maths.arithmatics";
	private String xmlFileName = "arithmatics.xml";
	
	public List<String> getOperations(int grade) {
		List<String> operations = new ArrayList<String>();
		Arithmatics arithmatics = (Arithmatics) loadedXmlData();
		List<Grade> grades = arithmatics.getGrade();
		for(Grade gra_de: grades) {
			if(gra_de.getGrade() == grade) {
				List<Info> infos = gra_de.getInfo();
				for(Info info: infos) {
					operations.add(info.getOperation());
				}
				return operations;
			}
		}
		return operations;
	}
	
	public BaseData getArithmaticData(int grade, String category) {
		Arithmatics arithmatics = (Arithmatics) loadedXmlData();
		List<Grade> grades = arithmatics.getGrade();
		for(Grade gra_de: grades) {
			if(gra_de.getGrade() == grade) {
				List<Info> infos = gra_de.getInfo();
				for(Info info: infos) {
					if(info.getOperation().equalsIgnoreCase(category)) {
						return this.extractBaseData(info);
					}
				}
			}
		}
		return null;
	}
	
	@Override
	public String getContextPath() {
		return CONTEXT_PATH;
	}

	@Override
	public String getXmlFileName() {
		return xmlFileName;
	}
	
	private BaseData extractBaseData(Info info) {
		BaseData basedata = new BaseData();
		Digits digits = info.getDigits();
		basedata.setDigits(RandomNumberGenerator.generate(digits.getFrom(), digits.getTo())); 
		Decimals decimals = info.getDecimals();
		basedata.setDecimals(RandomNumberGenerator.generate(decimals.getFrom(), decimals.getTo()));
		Datarows datarows = info.getDatarows();
		basedata.setDatarows(RandomNumberGenerator.generate(datarows.getFrom(), datarows.getTo()));
		return basedata;
	}

/*public class XMLDataService {
	
	private static final String CONTEXT_PATH = "net.lwf.jaxb.stubs.maths.arithmatics";
	
	private XmlFileInfo xmlFileInfo;
	private String xmlFileName = "arithmatics.xml";
	
	private Arithmatics arithmatics;
	
	public XMLDataService() {
		this.loadXmlData();
	}
	
	public BaseData getArithmaticData(int grade, String category) {
		this.loadXml();
		List<Grade> grades = this.arithmatics.getGrade();
		for(Grade gra_de: grades) {
			if(gra_de.getGrade() == grade) {
				List<Info> infos = gra_de.getInfo();
				for(Info info: infos) {
					if(info.getOperation().equalsIgnoreCase(category)) {
						return this.extractBaseData(info);
					}
				}
			}
		}
		return null;
	}
	
	private BaseData extractBaseData(Info info) {
		BaseData basedata = new BaseData();
		Digits digits = info.getDigits();
		basedata.setDigits(RandomNumberGenerator.generate(digits.getFrom(), digits.getTo())); 
		Decimals decimals = info.getDecimals();
		basedata.setDecimals(RandomNumberGenerator.generate(decimals.getFrom(), decimals.getTo()));
		Datarows datarows = info.getDatarows();
		basedata.setDatarows(RandomNumberGenerator.generate(datarows.getFrom(), datarows.getTo()));
		return basedata;
	}
	
	private void loadXmlData() {
		loadXml();
		try {
			JAXBContext jc = JAXBContext.newInstance(CONTEXT_PATH);
			Unmarshaller u = jc.createUnmarshaller();
			arithmatics = (Arithmatics) u.unmarshal(xmlFileInfo.getInputStream());
		} catch (Exception exc) {
			exc.printStackTrace();
		}
	}
	
	private synchronized void loadXml() {
		if (xmlFileInfo == null) {
			xmlFileInfo = new XmlFileInfo(xmlFileName);
		} else {
			if (xmlFileInfo.isChanged() == false) {
				return;
			}
		}
	}*/
	
}
